from pandac.PandaModules import *                       # Basic Panda modules
from direct.showbase.DirectObject import DirectObject   # For event handling
from direct.actor.Actor import Actor                    # For animated models
from direct.interval.IntervalGlobal import *            # For compound intervals
from direct.task import Task                            # For update functions
from direct.particles.ParticleEffect import ParticleEffect  #For particles
import random

class Door(object):
    def __init__(self, x = 0, y = 0, z = 0, orient = 0, id = 0, maxh = 3):
        #Health
        self.maxHealth = maxh
        self.curHealth = maxh
        
        #Position
        self.xPos = x
        self.yPos = y
        self.zPos = z
        self.orient = 90*(1-orient)
        
        #Door State
        self.destroyed = False
        self.debris = False
        
        #Model info
        self.node = loader.loadModel("Art/models/Door")
        self.node.setPos(self.xPos,self.yPos,self.zPos)
        self.node.setHpr(self.orient,0,0)
        self.node.setScale(1.2)
        self.node.reparentTo(render)
        self.node.hide()
        
        #setup visible model
        self.model = loader.loadModel("Art/models/Door")
        self.model.setPos(self.xPos,self.yPos,self.zPos)
        self.model.setHpr(self.orient,0,0)
        self.model.setScale(1.2)
        self.model.reparentTo(render)
        self.setupCollision()
        
        #Set up particle for collapsing
        self.collapseParticle = ParticleEffect()
        self.collapseParticle.loadConfig("Art/models/Door Poof.ptf")   
        self.collapseParticle.setPos(x,y,z)
        self.particleTime = 0
        
        self.id = id
       
    
    def damage(self):
        if self.debris == False:
            self.curHealth -= 1
            
            #TODO: I can't find a better way to re-load models...
            self.model.remove()
            
            if(self.curHealth == 2):
                self.model = loader.loadModel("Art/models/Door 1")
            if(self.curHealth == 1):
                self.model = loader.loadModel("Art/models/Door 2")
            if(self.curHealth == 0):
                self.destroyed = True
                self.collapseParticle.start(parent = render, renderParent = render)
                self.particleTime = 0
                return
            
            self.model.setPos(self.xPos,self.yPos,self.zPos)
            self.model.setHpr(self.orient,0,0)
            self.model.setScale(1.2)
            self.model.reparentTo(render)

    def roomCollapsed(self):
        self.debris = True
        self.model.remove()
        self.model = loader.loadModel("Art/models/Debris")
        self.model.setPos(self.xPos,self.yPos,self.zPos)
        self.model.setHpr(random.randint(0,360),0,0)
        self.model.setScale(1.2)
        self.model.reparentTo(render)
        self.collapseParticle.start(parent = render, renderParent = render)
        self.particleTime = 0
        
    def setupCollision(self):
        self.doorGroundSphere = CollisionTube(0, 0, 1, 0, 0, 7, 2)
        self.doorGroundCol = CollisionNode('doorTube') 
        self.doorGroundCol.addSolid(self.doorGroundSphere) 
        self.doorGroundColNp = self.node.attachNewNode(self.doorGroundCol)
        #self.doorGroundColNp.show()
        #self.doorGroundCol.setIntoCollideMask(BitMask32(0x1))
        
        base.door_dict[self.doorGroundColNp] = self